package com.itheima.web.service.impl.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.pms.ListWithAttrDto;
import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.pms.PmsProductAttributeCategoryService;
import com.itheima.service.pms.PmsProductAttributeService;
import com.itheima.service.pms.PmsProductCategoryService;
import com.itheima.vo.pms.PmsProductAttributeCategory;
import com.itheima.vo.pms.ProductAttributeParam;
import com.itheima.vo.pms.ProductCategoryParam;
import com.itheima.web.mapper.pms.PmsProductAttributeCategoryMapper;
import com.itheima.web.mapper.pms.PmsProductCategoryMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class PmsProductAttributeCategoryServiceImpl extends ServiceImpl<PmsProductAttributeCategoryMapper, PmsProductAttributeCategory> implements PmsProductAttributeCategoryService {
    @Autowired
    private PmsProductAttributeCategoryMapper pmsProductAttributeCategoryMapper;

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    /**
     * 添加商品属性分类
     * @param name
     * @return
     */
    @Override
    public void addProductAttributeCategory(String name) {
        PmsProductAttributeCategory pmsProductAttributeCategory = new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setName(name);
        pmsProductAttributeCategoryService.save(pmsProductAttributeCategory);
    }

    /**
     * 删除单个商品属性分类
     * @param id
     * @return
     */
    @Override
    public ResultData deleteProductAttributeCategory(Long id) {
        LambdaQueryWrapper<ProductAttributeParam> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttributeParam::getProductAttributeCategoryId , id);
        List<ProductAttributeParam> attributeParams = pmsProductAttributeService.list(queryWrapper);
        if (attributeParams.size() != 0) {
            return ResultData.error("该属性分类有下属属性，不能删除");
        }else {
            pmsProductAttributeCategoryService.removeById(id);
            return ResultData.ok("操作成功" , "");
        }
    }

    /**
     * 分页获取所有商品属性分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultData listProductAttributeCategory(Long pageNum, Long pageSize) {
        Page<PmsProductAttributeCategory> pageInfo = new Page<>(pageNum , pageSize);
        pmsProductAttributeCategoryService.page(pageInfo);
        PageResult pageResult = PageResult.init(pageInfo.getTotal(), pageSize, pageNum, pageInfo.getRecords());
        return ResultData.ok("操作成功" , pageResult);
    }

    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @Override
    public List<ListWithAttrDto> listWithAttr() {
        List<PmsProductAttributeCategory> list = pmsProductAttributeCategoryService.list();
        List<ListWithAttrDto> listWithAttrDtoList = list.stream().map(item -> {
            ListWithAttrDto listWithAttrDto = new ListWithAttrDto();
            BeanUtils.copyProperties(item , listWithAttrDto);
            LambdaQueryWrapper<ProductAttributeParam> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductAttributeParam::getProductAttributeCategoryId , item.getId());
            listWithAttrDto.setProductAttributeParams(pmsProductAttributeService.list(queryWrapper));
            return listWithAttrDto;
        }).collect(Collectors.toList());
        return listWithAttrDtoList;
    }

    /**
     * 修改商品属性分类
     * @param id
     * @param name
     * @return
     */
    @Override
    public void updateProductAttributeCategory(Long id, String name) {
        PmsProductAttributeCategory pmsProductAttributeCategory = pmsProductAttributeCategoryService.getById(id);
        pmsProductAttributeCategory.setName(name);
        pmsProductAttributeCategoryService.updateById(pmsProductAttributeCategory);
    }

    /**
     * 获取单个商品属性分类信息
     * @param id
     * @return
     */
    @Override
    public PmsProductAttributeCategory categoryId(Long id) {
        return pmsProductAttributeCategoryService.getById(id);
    }
}
